extends Resource
class_name FileTool

const _avatar_base_path = "res://Art/Human/"

static func load_story()->Array[StoryEntity]:
	var file = FileAccess.open("res://Resources/InitData/story.json",FileAccess.READ)
	var text = file.get_as_text()
	file.close()
	var json:Array = JSON.parse_string(text)
	if json == null:
		printerr("解析失败")
		pass
	var result:Array[StoryEntity] = []
	for item:Dictionary in json:
		var obj = StoryEntity.new()
		obj.story_id = item["story_id"]
		obj.story_name = item["story_name"]
		obj.story_des = item["story_des"]
		var fids = JSON.parse_string(item["faction_ids"])
		#obj.faction_ids
		for id:int in fids:
			obj.faction_ids.push_back(id)
			pass
		
		result.push_back(obj)
		pass
		
	return result
	pass

static func read_file(path:String)->String:
	var file = FileAccess.open(path,FileAccess.READ)
	var text = file.get_as_text()
	file.close()
	return text
	pass
	
static func load_hero_list()->Array[HeroEntity]:
	var text = read_file("res://Resources/InitData/heros.json")
	var json = JSON.parse_string(text)
	var result:Array[HeroEntity] = []
	for item:Dictionary in json:
		var obj = HeroEntity.new()
		obj.hero_id = item["hero_id"] as int		
		obj.hero_name = item["hero_name"]
		obj.level = item["level"] as int
		obj.exp = item["exp"] as float
		obj.devotion = item["devotion"] as int
		obj.base_wushu = item["base_wushu"] as int
		obj.base_agility = item["base_agility"] as int
		obj.base_intelligence = item["base_intelligence"] as int
		obj.base_commandry = item["base_commandry"] as int
		obj.base_beauty = item["base_beauty"] as int
		obj.base_morality = item["base_morality"] as int
		obj.underling_value = item["underling_value"] as int
		obj.hero_avatar = item["hero_avatar"] as String
		obj.hero_avatar_texture = load(_avatar_base_path + obj.hero_avatar) as Texture2D
		result.push_back(obj)
		pass
	return result
	pass
	
static func find_hero_by_id(id:int)->HeroEntity:
	var list:Array[HeroEntity] = load_hero_list()
	var tar:HeroEntity
	for h in list:
		if h.hero_id == id:
			tar = h
			break
		pass
	return tar
	pass
	
static func load_cities()->Array[CityEntity]:
	var text = read_file("res://Resources/InitData/city.json")
	var json = JSON.parse_string(text)
	
	var result:Array[CityEntity] = []
	for item:Dictionary in json:
		var obj = CityEntity.new()
		obj.city_id = item["city_id"] as int
		obj.city_name = item["city_name"]
		obj.population_value = item["population_value"]
		obj.gold_value = item["gold_value"]
		obj.food_value = item["food_value"]
		obj.farm_value = item["farm_value"]
		obj.business_value = item["business_value"]
		obj.people_happiness_value = item["people_happiness_value"]
		obj.disaster_stop_value = item["disaster_stop_value"]
		obj.standholder_id = item["standholder_id"]
		obj.city_position = Vector2(item["city_pos_x"] as float,item["city_pos_y"] as float)
		var city_ids = JSON.parse_string(item["hero_list_ids"]) as Array[int]
		for city_id:int in city_ids:
			var h = find_hero_by_id(city_id)
			obj.hero_list.push_back(h)
			pass
		result.push_back(obj)
		pass
	return result
	pass
	
static func find_city_by_id(id:int)->CityEntity:
	var cities:Array[CityEntity] = load_cities()
	var tar:CityEntity
	for c in cities:
		if c.city_id == id:
			tar = c
			break
		pass
	return tar
	pass
	
static func load_faction()->Array[FactionEntity]:
	var text = read_file("res://Resources/InitData/faction.json")
	var json:Array = JSON.parse_string(text)
	if json == null:
		print("parse error")
		pass
	
	var result : Array[FactionEntity] = []
	for item:Dictionary in json:
		var obj = FactionEntity.new()
		obj.faction_id = item["faction_id"] as int
		obj.king_id = item["king_id"] as int
		obj.faction_name = item["faction_name"]
		var city_ids = JSON.parse_string(item["city_list_id"]) as Array[int]
		for c_id:int in city_ids:
			var c = find_city_by_id(c_id)
			c.faction_id = obj.faction_id
			obj.city_list.push_back(c)
			pass
		result.push_back(obj)
		pass
	return result
	pass
	
static func find_faction_by_id(id:int)->FactionEntity:
	var factions:Array[FactionEntity] = load_faction()
	var tar : FactionEntity
	for f in factions:
		if f.faction_id == id:
			tar = f
			break
			pass
		pass
	return tar
	pass
	
static func get_faction_list_by_ids(ids:Array[int])->Array[FactionEntity]:
	var result:Array[FactionEntity] = []
	for id in ids:
		result.push_back(find_faction_by_id(id))
		pass
	return result
	pass
